﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Activities.Presentation.Model;
using System.Activities;
using Microsoft.VisualBasic.Activities;
using TransactionServer.General_Classes;

namespace TransactionServer.ActivityDesign.Helpers
{
    class EntityListVariableConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            ModelItem modelItem = value as ModelItem;
            if (value != null)
            {

                if (modelItem.GetCurrentValue() is OutArgument<List<Entity>>)
                {
                    OutArgument<List<Entity>> outArgument = modelItem.GetCurrentValue() as OutArgument<List<Entity>>;

                    if (outArgument != null)
                    {
                        Activity<Location<List<Entity>>> expression = outArgument.Expression;
                        VisualBasicReference<List<Entity>> vbexpression = expression as VisualBasicReference<List<Entity>>;

                        if (vbexpression != null)
                        {
                            return vbexpression.ExpressionText;
                        }
                    }
                }
                else if (modelItem.GetCurrentValue() is InArgument<List<Entity>>)
                {
                    InArgument<List<Entity>> outArgument = modelItem.GetCurrentValue() as InArgument<List<Entity>>;

                    if (outArgument != null)
                    {
                        Activity<List<Entity>> expression = outArgument.Expression;
                        VisualBasicValue<List<Entity>> vbexpression = expression as VisualBasicValue<List<Entity>>;

                        if (vbexpression != null)
                        {
                            return vbexpression.ExpressionText;
                        }
                    }
                }

            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            if (targetType == typeof(OutArgument<List<Entity>>))
            {
                VisualBasicReference<List<Entity>> vbArgument = new VisualBasicReference<List<Entity>>(value.ToString());
                return new OutArgument<List<Entity>>(vbArgument);
            }
            else if (targetType == typeof(InArgument<List<Entity>>))
            {
                VisualBasicValue<List<Entity>> vbArgument = new VisualBasicValue<List<Entity>>(value.ToString());
                return InArgument<List<Entity>>.FromExpression(vbArgument);
            }

            return null;
        }
    }
}
